# -*- coding: utf-8 -*
import os
import urllib.request
class tool(object):

    def text_save_list(self,filename,data):
        '''
        将列表写入txt 文件
        #filename为写入文件的路径，data为要写入数据列表.
        '''
        file = open(filename, 'a', encoding='utf-8')
        for i in range(len(data)):
            s = str(data[i]).replace(
                '[', '').replace(']', '')  # 去除[],这两行按数据不同，可以选择
            s = s.replace("'", '').replace(',', '') + \
                '\n'  # 去除单引号，逗号，每行末尾追加换行符
            file.write(s)
        file.close()
        print("保存文件成功")

    def text_save_string(self,filename,data):
        '''
        将列表写入txt 文件
        #filename为写入文件的路径，data为要写入字符串.
        '''
        file = open(filename, 'a', encoding='utf-8')
        file.write(str(data) + '\n')
        file.close()
        print("保存文件成功")

    def downloadImg(self , filename , url , name):
        '''
        下载图片
        '''
        os.makedirs(filename , exist_ok=True)
        req = urllib.request.Request(url)
        response = urllib.request.urlopen(req)
        imgName = filename +"/" + str(name) + ".png"
        if (response.getcode() == 200):
            with open(imgName, "wb") as f:
                f.write(response.read()) # 将内容写入图片
            return imgName
               
          

